package com.leecode.string;

public class MaxPublicPrefixMain {
	public static void main(String[] args) {
		System.out.println(test(new String[]{"flower", "flow", "flight"}));
		System.out.println(test(new String[]{"dog", "racecar", "car"}));
	}

	public static String test(String[] strArr) {
		if (strArr.length==0) {
			return "";
		}
		String minStr = getMinStr(strArr);

		int maxIndex = getMaxIndex(strArr, minStr);

		return minStr.substring(0, maxIndex + 1);
	}

	private static int getMaxIndex(String[] strArr, String minStr) {
		int maxIndex = -1;
		char[] chars = minStr.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			for (int j = 0; j < strArr.length; j++) {
				if (strArr[j].charAt(i) != chars[i]) {
					//真.直接GG
					return maxIndex;
				}
			}
			//每个str的index=i的字符都合chars的字符相同
			maxIndex++;
		}
		return maxIndex;
	}

	public static String getMinStr(String[] strArr) {
		int min = 0;

		//
		for (int i = 1; i < strArr.length; i++) {
			if (strArr[i].length() < strArr[min].length()) {
				min = i;
			}
		}

		return strArr[min];
	}
}
